package treediff.wrapper;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

import org.htmlparser.util.ParserException;

public class TreeDiffFolderNavigator {

	private static String[] type = {"added","changed","removed","total"};
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws ParserException 
	 */
	public static void main(String[] args) throws ParserException, IOException {
		
		String stage = "training";
		
//		System.setOut(new PrintStream(new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/TEDW/out.txt")));
		
		File folder = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/BreakDetectorReloaded/" + stage + "/27/");

		File output = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/TEDWL/" + stage + "/");
		
		File[] dbfolders = folder.listFiles();
		
//		dbfolders = new File[1];
//		
//		dbfolders[0] = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/BreakDetectorReloaded/"+stage+"/27/540/");
		
		File resultsFolder = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/QueryResults/" + stage + "/");
		
		for (int i = 0; i < dbfolders.length; i++) {
			
			System.gc();
			
			String name = dbfolders[i].getName();
			
			System.out.println("DATABASE: " + name);
			
			File outp = new File(output,name);
			
			if (outp.exists())
				continue;
			
			outp.mkdir();
			
			File toiterate = new File(resultsFolder,name);
			
			File[] results = toiterate.listFiles();
			
			for (int j = 0; j < type.length; j++) {
				
				String typ = type[j];

				System.out.println("TYPE: " + typ);
				
				File wrap = new File(folder,name + "/" + name + "." + typ + ".break");
				
				for (int k = 0; k < results.length; k++) {
					
					System.out.println("FILE: " + results[k]);
					
					File outputFile = new File(outp,typ+ "." +results[k].getName());
					
					new TreeEditDistanceWrapper().extract(wrap,results[k],outputFile);
					
				}
				
			}
			
		}
		
	}

}
